import React from 'react'
import { Router, Route, Switch } from 'react-router-dom';
import { connect } from 'react-redux';
import history from'../history/history';
import Home from '../containers/Home/Home';
import Reservation from '../containers/Reservation/Reservation';
import UserCenter from '../containers/UserCenter/UserCenter';
import NotFound from '../containers/404/404';

class RouterMap extends React.Component {
    componentWillMount() {
        if(!this.props.hasOpen) {
            history.push('/');
        }
    }

  render() {
    return(
        <Router history={history}>
            <Switch>
                <Route exact path="/" component={Home} />
                <Route path="/reservation" component={Reservation} /> 
                <Route path="/userCenter" component={UserCenter} /> 
                <Route path="*" component={NotFound} />
            </Switch>
        </Router>
    )
  }
}

const mapStateToProps = (state, ownProps = {}) => {
    return Object.assign(ownProps, {
        hasOpen: state.hasOpen
    });
}

const mapDispatchToProps = (dispatch) => {
    return {}
}

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(RouterMap);